package com.forum.service.impl;

import com.forum.dao.LoginDao;
import com.forum.entity.Result;
import com.forum.entity.User;
import com.forum.service.LoginService;
import com.forum.util.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service("LoginService")
public class LoginServiceImpl implements LoginService {
    @Resource
    LoginDao loginDao;


    @Override
    public User login(String account, String password) {
        return loginDao.getByUsernameAndPassword(account, password);
    }

    /**
     * 注册账号
     */
    @Override
    public Result<User> regedit(User user) {
        int usersCount = loginDao.select(user);

        if (usersCount != 0) {
            return Result.fail("账号已存在");
        } else if (user.getUserName() == null || user.getUserName().equals("")) {
            return Result.fail("未填写昵称");
        } else if (user.getAccount() == null || user.getAccount().equals("")) {
            return Result.fail("未填写账号");
        } else if (user.getPassword() == null || user.getPassword().equals("")) {
            return Result.fail("未填写密码");
        }

        user.setPassword(user.getPassword());
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        loginDao.regedit(user);
        return Result.ok("注册成功！");
    }

}
